package com.zhj.edu.result;

import lombok.Data;

import java.util.*;

@Data
public class R {

    //响应状态码
    private Integer code;

    //错误的提示信息
    private String msg;

    //响应给客户端的数据
    private Map<String, Object> dataMap = new HashMap<>();

    //私有化，禁止别人new这个对象
    private R(){}

    public static R ok(){
        R r = new R();
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMsg(ResponseCode.SUCCESS.getMsg());
        return r;
    }

    public static R error(){
        R r = new R();
        r.setCode(ResponseCode.FAIL.getCode());
        r.setMsg(ResponseCode.FAIL.getMsg());
        return r;
    }

    public static R error(ResponseCode responseCode){
        R r = new R();
        r.setCode(responseCode.getCode());
        r.setMsg(responseCode.getMsg());
        return r;
    }

    public static R error(Integer code,String msg){
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    /**
     * 放数据的问题
     * @param key
     * @param val
     * @return
     */
    public R data(String key, Object val){
        this.dataMap.put(key, val);
        return this;
    }

    public R data(Map<String, Object> maps){
        Set<Map.Entry<String, Object>> entries = maps.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            this.dataMap.put(next.getKey(), next.getValue());
        }
        return this;
    }
}
